//
//  ES1Renderer.h
//  iPadStencilTest
//
//  Created by Goffredo Marocchi on 8/19/10.
//  Copyright IGGS 2010. All rights reserved.
//

#import "ESRenderer.h"
#import "RetrieveGLRenderer.h"
#import "EAGLView.h"

#import "ShadowMatrix.h"
#import "Projector.h"

#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import "sio2.h"

@interface SIO2GL : NSObject <ESRenderer>
{
@private
    EAGLContext *context;

    // The pixel dimensions of the CAEAGLLayer
    GLint backingWidth;
    GLint backingHeight;

    // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view
    GLuint defaultFramebuffer, colorRenderbuffer, depthRenderbuffer, stencilRenderbuffer, depthStencilBuffer;
	BOOL isSimulator;
	BOOL displayLinkSupported;
	BOOL isAnimating;
	id displayLink;
	NSTimer *animationTimer;
	
	double timeStampPrevious;
	
	//TODO: implement a state manager overseeing camera and other elements
	BOOL updateCameraProjectionMatrix;
	BOOL updateCameraModelViewMatrix;
	//
	
	NSInteger animationFrameInterval;
	ShadowMatrix* shadowMatrix;
	Projector* sceneProjector;
	
	SIO2object* objShadowReceiver[MAX_SHADOW_RECEIVERS];
	
	vec3 temp_camera_loc;
	vec3 temp_camera_target;
	
	Average* frameTimeAVG;
}
@property (nonatomic, retain) EAGLView* glView;
@property (nonatomic) NSInteger animationFrameInterval; 

@property (copy) ShadowMatrix* shadowMatrix;
@property (copy) Projector* sceneProjector;
@property (nonatomic) BOOL updateCameraProjectionMatrix;
@property (nonatomic) BOOL updateCameraModelViewMatrix;

- (void)startAnimation;
- (void)stopAnimation;

- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer;
-(void) setObjShadowReceiver:(SIO2object **)obj;

-(void) makeGLViewTrasparent;
-(void) makeGLViewOpaque;

-(UIImage*) screenshot;

/////////////////////////////////////////////////////////

+(int)  getTriangleCountFromObject:(SIO2object*)obj;
+(void) calculateTriangleNormal:(vec3*)normal v0:(const vec3*)v0 v1:(const vec3*)v1 v2:(const vec3*)v2;

+(void) matPerspective:(float*)m withFov:(float)fovYdeg aspectRatio:(float)aRatio zNear:(float)zN zFar:(float)zF portrait:(BOOL)rot 
		zRangeInverted:(BOOL)inverted;
+(void) matPerspective:(float*)m withFov:(float)fovYdeg aspectRatio:(float)aRatio zNear:(float)zN zFar:(float)zF 
			  portrait:(BOOL)rot;
+(void) matPerspectiveFPz:(float*)m withFov:(float)fovYdeg aspectRatio:(float)aRatio zNear:(float)zN portrait:(BOOL)rot 
		   zRangeInverted:(BOOL)inverted;
+(void) matPerspectiveFPz:(float*)m withFov:(float)fovYdeg aspectRatio:(float)aRatio zNear:(float)zN 
				 portrait:(BOOL)rot;
+(void) matLookAt:(MATRIX*)m eye:(vec3*)_e vTarget:(vec3*)_c upVector:(vec3*)_u;
+(void) matLookAt:(MATRIX*)m eye:(vec3*)_e vDir:(vec3*)_d upVector:(vec3*)_u;
+(void) vec3LerpVec1:(vec3*)v1 vec2:(vec3*)v2 withCoeff:(float)s intoVec:(vec3*)vOut;
+(BOOL) isSIO2ObjectName:(NSString*)str InSIO2ObjectArray:(SIO2object**)obj ofSize:(int)size;

+(void) drawNormal:(vec3*)normal ofPoint:(vec3*)p;
+(vec3) travelAlongNormal:(vec3*)normal fromPoint:(vec3*)p1 distance:(float)dist;
+(void) drawLineFrom:(vec3 *)fromPoint to:(vec3 *)toPoint withColor:(GLubyte*)color; //DEBUG function

/////////////////////////////////////////////////////////

-(void) getTriangleList:(vec3*)trianglePtr fromObject:(SIO2object*)obj;
-(void) calculatePlaneNormal:(SIO2object *)plane withCam:(const SIO2camera*)cam withNormal:(vec3*)normal;
-(void) renderSIO2ObjectNormal:(char*) objString withSIO2:(SIO2*)sio2;

-(void) setupLighting;
-(void) loadSIO2Camera:(const char*)camera;

-(void) cameraUpdate:(SIO2camera*) _SIO2camera;
-(void) renderCameraProjectionMatrix:(MATRIX*)mProjection;
-(void) renderCameraModelViewMatrix:(MATRIX*)mView enableLandscape:(BOOL)landscape;
-(void) drawCameraFrustum:(SIO2camera*)cam aspectRatio:(float)aspect;

-(void) enableClipPlane:(int)clipPlaneIndex withPlaneNormal:(vec3*)normal withOffset:(float)offset;
-(void) disableClipPlane:(int)clipPlaneIndex;

-(void) renderAllObjects;
-(void) renderShadowReceiverObject:(SIO2object*)obj;
-(void) renderShadowCasters;
-(void) renderLights;
-(void) renderTransparentObjects;
-(void) renderIPO;

-(void) loadImageSetTexture:(NSString*)image withId:(NSString*)texId;

-(CFTimeInterval) getCADLTimeStamp;
-(CFTimeInterval) getCADLDuration;
-(unsigned int) getCADLFrameInterval;

/////////////////////////////////////////////////////////

@end
